#define _CRT_SECURE_NO_WARNINGS 1



 //Definition for a binary tree node.
 struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode() : val(0), left(nullptr), right(nullptr) {}
     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 };
class Solution {
public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        return _hasPathSum(root, targetSum, 0);
    }

    bool _hasPathSum(TreeNode* root, int targetSum, int sum)
    {
        if (root == nullptr)
            return false;
        sum += root->val;
        if (root->left == nullptr && root->right == nullptr)
        {
            return sum == targetSum;
        }
        if (root->left == nullptr)
        {
            return _hasPathSum(root->right, targetSum, sum);
        }
        if (root->right == nullptr)
        {
            return _hasPathSum(root->left, targetSum, sum);
        }
        return _hasPathSum(root->left, targetSum, sum) || _hasPathSum(root->right, targetSum, sum);
    }
};